package com.packet.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.packet.pojo.Customs;
import com.packet.service.FinanceCorrectTripService;
import com.packet.service.GetFromStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className:FinanceCorrectController
 * @Author:wallet_packet Date:2023-12-06 11:04
 * @Describe:
 */
@Controller
public class FinanceCorrectController {
    private  Integer times = 0;
    @Autowired
    FinanceCorrectTripService financeCorrectTripService;
    @Autowired
    GetFromStaffService getFromStaffService;

    @RequestMapping("/finance/queryDetailById/{id}")
    public String queryDetail(Model model, @PathVariable("id") Integer id,HttpSession session){
        //List<Customs> customs = financeCorrectTripService.selectDetailById(id);
        System.out.println(id);
        List<Customs> customs = getFromStaffService.getCustoms(id);
        System.out.println(customs);
        model.addAttribute("customs",customs);
        session.setAttribute("bid",id);
        float sumMoney = financeCorrectTripService.countAllMoney();
        model.addAttribute("sumMoney",sumMoney);
        System.out.println(times);
        if(times!=0){
            model.addAttribute("refuse","refuse");
            times = 0;
        }
        return "/finance/detail";
    }

    //审批通过
    @RequestMapping("/finance/correctIt")
    public String correctItById(HttpSession session){
        Integer sid = (Integer) session.getAttribute("sid");
        Integer bid = (Integer) session.getAttribute("bid");
        Map<String,Object> map = new HashMap<>();
        map.put("state",6);
        map.put("result","同意");
        map.put("detail","");
        map.put("bid",bid);
        map.put("sid",sid);
        boolean endResult = financeCorrectTripService.correctItById(map);
        boolean stateResult = financeCorrectTripService.correctState(map);
        return "redirect:/finance/queryNoCorrect";
    }

    /**
     * 传入参数，如果是第一次，就直接返回/finance/detail，显示隐藏内容
     * 第二次就 存入数据库
     * @return
     */
    @RequestMapping("/finance/returnBack")
    public String returnBack(Model model, HttpSession session, RedirectAttributes attributes){
        model.addAttribute("refuse","refuse");
        Integer bid = (Integer) session.getAttribute("bid");
        attributes.addAttribute("bid",bid);
        times ++;
        return "redirect:/finance/queryDetailById/{bid}";
    }

    @RequestMapping("/finance/refuseIt")
    public String refuseIt(HttpSession session,@RequestParam("detail") String detail){
        System.out.println(detail);
        Integer sid = (Integer) session.getAttribute("sid");
        Integer bid = (Integer) session.getAttribute("bid");
        Map<String,Object> map = new HashMap<>();
        map.put("state",9);
        map.put("result","拒绝");
        map.put("detail",detail);
        map.put("bid",bid);
        map.put("sid",sid);
        boolean endResult = financeCorrectTripService.correctItById(map);
        boolean stateResult = financeCorrectTripService.correctState(map);
        return "redirect:/finance/queryNoCorrect";
    }
}
